package fiuba.descuentos;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Vector;

import org.json.JSONObject;

import Helpers.ContextHelper;
import Listeners.CustomLocationListener;
import Service.IService;
import Service.RestServiceFactory;
import Service.WellKnownMethods;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class ActivityBase extends Activity implements IService {
	private ProgressDialog dialog;
	protected int orientation = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);		
		ContextHelper.setContextInstance(this);
		CustomLocationListener.startListening();
		this.orientation = getResources().getConfiguration().orientation;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ContextHelper.setContextInstance(this);
	}
	
	public void showLoading(Context context, String message) {
		if (message == null)
			message = getResources().getString(R.string.loading); 
		dialog = ProgressDialog.show(context, "", message, true);
		dialog.show();
	}

	public void hideLoading() {
		if (dialog != null) {
			dialog.hide();
		}
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;
		switch (item.getItemId()) {
			case R.id.text_search:
				intent = new Intent(this, TextSearch.class);
				break;
			case R.id.categories:
				intent = new Intent(this, Categories.class);
				break;
			case R.id.map:
				intent = new Intent(this, Map.class);
				break;			
			case R.id.login:
				intent = new Intent(this, Login.class);
				break;
			case R.id.logout:
				ContextHelper.setUserId(-1);
				intent = new Intent(this, Login.class);
				break;	
			case R.id.configuration:
				intent = new Intent(this, Configuration.class);
				break;
			case R.id.top_ranked:
				intent = new Intent(this, TopBenefit.class);
				break;	
		}
		if (intent != null){			
			startActivity(intent);
			return true;
		}
		return false;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		if(ContextHelper.getUserId() > 0){
			inflater.inflate(R.menu.logoutmenu, menu);
		}else{
			inflater.inflate(R.menu.loginmenu, menu);
		}
			
		return true;
	}
	
	@Override
	public void callService(String method, Vector props) {
		callService(method, props, true);			
	}
	
	public void callService(String method, Vector props, Boolean loading) {
		if(loading){
			showLoading(this, null);
		}
		RestServiceFactory.callService(this, method, props);		
	}

	@Override
	public void onError(String error) {
		hideLoading();
		showErrorMessage(error);		
	}

	@Override
	public void onServiceCompleted(JSONObject obj, String method) {
		
	}
	
	protected void showErrorMessage(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
}